#!/usr/bin/env python
# coding:utf-8


from PyQt4 import QtGui
from sql_rice import rules


class LabeledWidget(QtGui.QWidget):
    """Widget acompañado de una etiquete descriptiva"""
    
    def __init__(self,widget_info, parent, value_widget=QtGui.QLineEdit):
        QtGui.QWidget.__init__(self, parent)
        self.name = widget_info['name']
        self.info = widget_info
        self.setLayout(QtGui.QHBoxLayout(self))
        self.value_widget = value_widget(self)
        self.label_widget = QtGui.QLabel(self)
        self.label_widget.setText(self.name)
        self.layout().addWidget(self.label_widget)
        self.layout().addItem(QtGui.QSpacerItem(0, 0,
                QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum))
        self.layout().addWidget(self.value_widget)
        self.validators = []
        for rule, params in list(self.info['rules'].items()):
            r = rules.find(rule, params)
            if r.use_as == 'setup':
                r(self)
            elif r.use_as == 'valide':
                self.validators.append(r)
                
    def valide(self):
        """valide()->Bool
        return False if any of the validators is False otherwise return True"""
        return reduce(lambda f1, f2: f1(self.value) and f2(self.value), self.validators)


class TextWidget(LabeledWidget):
    """Widget de caja de texto"""
    
    def __init__(self, widget_info, parent):
        LabeledWidget.__init__(self, widget_info, parent)
        

    @property
    def read_only(self):
        return self.value_widget.readOnly()

    @read_only.setter
    def read_only(self, value):
        self.value_widget.setReadOnly(value)
        
    @property
    def value(self):
        return str(self.value_widget.text())
    
    @value.setter
    def value(self, value):
        self.value_widget.setText(value) 

        
class DateWidget(LabeledWidget):
    
    def __init__(self, widget_info, parent):
        LabeledWidget.__init__(self, widget_info, parent, QtGui.QDateEdit)
        
    @property
    def value(self):
        return self.value_widget.date().toPyDate()
      
    @value.setter
    def value(self, value):
        self.value_widget.setDate(value.year, value.month, value.day)
        

class RangeWidget(LabeledWidget):
    
    def __init__(self, widget_info, parent):
        LabeledWidget.__init__(self, widget_info, parent, QtGui.QSpinBox)
        
    @property
    def value(self):
        return self.value_widget.value()
        
    @value.setter
    def value(self, value):
        self.value_widget.setValue(value)
        
    @property
    def maximum(self):
        return self.value_widget.maximum()
    
    @maximum.setter
    def maximum(self, value):
        self.value_widget.setMaximum(value)
    
    @property
    def minimum(self):
        return self.value_widget.minimum()
    
    @minimum.setter
    def minimum(self, value):
        self.value_widget.setMinimum(value)


class EnumWidget(LabeledWidget):
    
    def __init__(self, widget_info, parent):
        LabeledWidget.__init__(self, widget_info, parent, QtGui.QComboBox)
        self.items = widget_info['items']
        assert isinstance(self.items, dict)
        
    @property
    def value(self):
        return self.items[str(self.value_widget.currentText())]
    
    @value.setter
    def value(self, value):
        idx = self.value_widget.findText(value)
        if idx < 0:
            raise KeyError('Enum has no value %s' % value)
        self.value_widget.setSelectedIndex(idx)
        